#!/bin/sh

if ! [ -s ~/.cache/.rss.cache ]; then
	echo "0" > ~/.cache/.rss.cache
fi
critical_run=$(pgrep "^newsboat$")
while  [ -n "$critical_run" ]; do
	sleep 20s
	critical_run=$(pgrep "^newsboat$")
done
notifications=$(newsboat -x reload -x print-unread | grep -Eo '[0-9]+')
if [ "$notifications" -eq 0 ]; then
	echo "0" | tee ~/.cache/.rss.cache
	exit 0;	
fi
previous=$(cat ~/.cache/.rss.cache)
if [ "$notifications" -gt "$previous" ]; then
	dunstify -i data-information -a "Newsboat" "You got $((notifications - previous)) new RSS feeds!"
	pw-play /usr/share/sounds/ITGC/notify.flac
	echo "$notifications" > ~/.cache/.rss.cache
fi
echo "$notifications" | tee ~/.cache/.rss.cache
